package com.kcsj.service;

import com.kcsj.dao.DelayedApplicationDao;
import com.kcsj.domain.Count;
import com.kcsj.domain.DelayedApplication;
import com.kcsj.domain.user.Customer;
import com.kcsj.domain.user.Notice;
import com.kcsj.domain.user.User;
import com.kcsj.service.user.UserService;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
/**
 * @author 傅立贤
 * 29th Nov. 2020
 */
public class DelayedApplicationService {
    private DelayedApplicationDao delayedApplicationDao = DelayedApplicationDao.getInstance();
    private static DelayedApplicationService delayedApplicationService
            = new DelayedApplicationService();

    public DelayedApplicationService() {
    }
    public static DelayedApplicationService getInstance(){
        return delayedApplicationService;
    }

    public boolean add(DelayedApplication delayedApplication) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Notice notice = new Notice("销售","客户订单延迟通知,订单号："
                +delayedApplication.getOrder().getOrderNo(),"未读");
        NoticeService.getInstance().add(notice);
        boolean added = delayedApplicationDao.add(delayedApplication, conn);
        JdbcHelper.close(conn);
        return added;
    }

    public Collection<Count> countByStatus() throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Count> counts = delayedApplicationDao.countByStatus(conn);
        JdbcHelper.close(conn);
        return counts;
    }

    public Collection<Count> countByDay() throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Count> counts = delayedApplicationDao.countByDay(conn);
        JdbcHelper.close(conn);
        return counts;
    }

    public Collection<DelayedApplication> findAll(List<Condition> conditions) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<DelayedApplication> delayedApplications =
                delayedApplicationDao.findAll(conditions, conn);
        JdbcHelper.close(conn);
        return delayedApplications;
    }
    public boolean update(DelayedApplication delayedApplication) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Customer customer = delayedApplication.getOrder().getCustomer();
        User user = UserService.getInstance().find(customer.getId());
        if(delayedApplication.getApplicationStatus().equals("延期中")){
            Notice notice = new Notice("客户","延期已同意,订单号："+delayedApplication.getOrder().getOrderNo(),"未读",user);
            NoticeService.getInstance().add(notice);
        }else if(delayedApplication.getApplicationStatus().equals("已驳回")){
            Notice notice = new Notice("客户","延期已驳回,订单号："+delayedApplication.getOrder().getOrderNo(),"未读",user);
            NoticeService.getInstance().add(notice);
        }
        boolean updated = delayedApplicationDao.update(delayedApplication, conn);
        JdbcHelper.close(conn);
        return updated;
    }
    public DelayedApplication find(String orderNo) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        DelayedApplication delayedApplication = delayedApplicationDao.find(orderNo, conn);
        return delayedApplication;
    }
    public boolean delete(String orderNo) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        boolean deleted = delayedApplicationDao.delete(orderNo, connection);
        JdbcHelper.close(connection);
        return deleted;
    }
}
